package leave.dataaccess.mysql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import leave.dataaccess.EmployeeCompensationLeaveDAO;

public class EmployeeCompensationLeaveDAOImpl implements
		EmployeeCompensationLeaveDAO {
	private Connection factoryConnection;

	public EmployeeCompensationLeaveDAOImpl(Connection factoryConnection) {
		super();
		this.factoryConnection = factoryConnection;
	}

	@Override
	public void insertCompensationLeave(int employee_id, int year, float days) {
		String insertSql = "insert into employee_compensation_leave values("
				+ employee_id + "," + year + "," + days + ")";
		try {
			Statement st = factoryConnection.createStatement();
			st.executeUpdate(insertSql);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void updateCompensationLeave(int employee_id, int year, float days) {
		// TODO Auto-generated method stub
		String updateSql = "update employee_compensation_leave set calendar_year="
				+ year
				+ ", compensated_days="
				+ days
				+ " where employee_id="
				+ employee_id;
		try {
			Statement st = factoryConnection.createStatement();
			st.executeUpdate(updateSql);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public HashMap<Integer, Float> getCompensationDays(int employee_id) {
		// TODO Auto-generated method stub
		HashMap<Integer, Float> mapping = new HashMap<Integer, Float>();
		String selectSql = "select * from employee_compensation_leave where employee_id="
				+ employee_id;
		ResultSet rs = null;
		try {
			Statement st = factoryConnection.createStatement();
			rs = st.executeQuery(selectSql);
			while (rs.next()) {
				mapping.put(rs.getInt("calendar_year"),
						rs.getFloat("compensated_days"));

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return mapping;
	}
}
